package dao;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -06 -25
 * Time: 20:16
 */

import bean.Dish;
import bean.Order;
import util.DBUtil;
import util.OrderSystemException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 1.新增
 * 2.删除
 * 3.查询所有
 * 4.查询指定
 * 5.修改
 */
public class DishDao {
    public void add(Dish dish) throws OrderSystemException{
        Connection connection = DBUtil.getConnection();
        String sql = "insert into dishes values(?,?,?)";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1, dish.getDishId());
            statement.setString(2, dish.getDishName());
            statement.setInt(3, dish.getPrice());
            int ret = statement.executeUpdate();
            if (ret != 1) {
                throw new OrderSystemException("插入菜品失败");
            }
            System.out.println("插入菜品成功");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OrderSystemException("插入菜品失败");
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }
    public void delete(int dishId) throws OrderSystemException{
        Connection connection = DBUtil.getConnection();
        String sql = "delete from dishes where dishId=?";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1, dishId);
            int ret = statement.executeUpdate();
            if (ret != 1) {
                throw new OrderSystemException("删除菜品失败");
            }
            System.out.println("删除菜品成功");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OrderSystemException("删除菜品失败");
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }
    public void update(int dishId,int price) throws OrderSystemException{
        Connection connection = DBUtil.getConnection();
        String sql = "update dishes set price=? where dishId=?";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1, price);
            statement.setInt(2, dishId);
            int ret = statement.executeUpdate();
            if (ret != 1) {
                throw new OrderSystemException("更新菜品失败");
            }
            System.out.println("更新菜品成功");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OrderSystemException("更新菜品失败");
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }
    public List<Dish> selectAll() throws OrderSystemException {
        List<Dish> dishes = new ArrayList<>();
        Connection connection = DBUtil.getConnection();
        String sql = "select * from dishes";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Dish dish = new Dish(resultSet.getInt("dishId"),
                        resultSet.getString("dishName"),
                        resultSet.getInt("price"));
                dishes.add(dish);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OrderSystemException("查询所有菜品失败");
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return dishes;
    }
    public Dish selectById(int dishId) throws OrderSystemException {
        List<Dish> dishes = new ArrayList<>();
        Connection connection = DBUtil.getConnection();
        String sql = "select * from dishes where dishId=?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,dishId);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                Dish dish = new Dish(resultSet.getInt("dishId"),
                        resultSet.getString("dishName"),
                        resultSet.getInt("price"));
                return dish;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OrderSystemException("查询所有菜品失败");
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }
    public static void main(String[] args) throws OrderSystemException {
        DishDao dishDao = new DishDao();
         //1. 测试新增
//        Dish dish = new Dish();
//        dish.setDishName("红烧肉");
//        dish.setPrice(3000); // 单位是 分
//        dishDao.add(dish);
//        Dish dish1 = new Dish();
//        dish1.setDishName("茄子肉沫");
//        dish1.setPrice(2000);
//        dishDao.add(dish1);
//        Dish dish3 = new Dish();
//        dish3.setDishName("手撕包菜");
//        dish3.setPrice(1500);
//        dishDao.add(dish3);
//         //2. 测试查找
//        List<Dish> dishes = dishDao.selectAll();
//        System.out.println("查看所有");
//        System.out.println(dishes);
//
//        Dish dish2 = dishDao.selectById(1);
//        System.out.println("查看单个");
//        System.out.println(dish1);

        // 3. 测试删除
        dishDao.delete(9);
        List<Dish> dishes = dishDao.selectAll();
        System.out.println("查看所有");
        System.out.println(dishes);
    }
}
